﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Brunch.Forms
{
	public partial class BreadCrumbSelectForm : BaseEditForm
	{
		private const string delim = " | ";
		public BreadCrumbSelectForm(string path, string selectedCrumb)
		{
			InitializeComponent();
			linkLabel.Text = string.Empty;
			crumb = selectedCrumb;
			ProcessPath(path);
		}

		private string crumb;
		public string SelectedCrumb
		{
			get { return crumb; }
		}
		public BrunchConfig GlobalConfig { get; set; }
		//public Delegate Continuation { get; set; }

		private void ProcessPath(string path)
		{
			if (string.IsNullOrEmpty(path))
				return;

			string[] parts = path.Split(Path.DirectorySeparatorChar);
			int start = 0, length;
			bool first = true;

			foreach (string part in parts)
			{
				if (first) { first = false; continue; }

				string breadCrumb = part + delim;
				length = part.Length;
				linkLabel.Text += breadCrumb;
				LinkLabel.Link lnk = linkLabel.Links.Add(start, length, part);

				//check current value
				if (!string.IsNullOrEmpty(crumb) && crumb == part)
					lnk.Visited = true;
				start += length + delim.Length;
			}

			linkLabel.Text = linkLabel.Text.Substring(0, linkLabel.Text.Length - delim.Length);
		}

		private void linkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			crumb = e.Link.LinkData as string;
			
			foreach (LinkLabel.Link lnk in linkLabel.Links)
				lnk.Visited = false;

			e.Link.Visited = true;
		}
	}
}
